import React from 'react';
import {Table, Row, Col, Form, Input, Icon, InputNumber, Radio, DatePicker, Select, Upload, Button, Switch, Modal, Popconfirm, Tabs, message} from 'antd';
import Constants from '../../../utils/Constants';
import EditBusinessagreement from '../components/EditBusinessagreement';
import RightBox from "../../commons/RightBox";
import RIGHT from "../../../utils/RightConstants";
import OperatorConstant from "../../../utils/OperatorConstant";
const R = require('ramda');
const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const Option = Select.Option;
const TabPane = Tabs.TabPane;

export default Form.create({wrappedComponentRef: true})(class BusinessAgreement extends React.Component {
    constructor(props) {
        super(props);
        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource
        this.state = {

        };
    }

    handleResetFields = () => {
        this.props.form.resetFields();
    }

    handleOpenAdd = () => {
        let customer  = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;
        if(customer.csttype === "1"){
            message.info("当前是会员客户，无需增加采购合同");
            return;
        }
        if( R.isNil(customer.fkmanager) || R.isEmpty(customer.fkmanager)){
            message.error("请先分配风控经理");
            return ;
        }

        if(this.editBusinessagreement)this.editBusinessagreement.handleOpen("add");
    }


    onEdit = (record, type) => {
        if(OperatorConstant.view == type){
            this.props.dispatch({type: 'businessAgreement/getCurrentNodeDetail', payload: {id: record.id, code: "agreement"}});
        }
        this.props.dispatch({type: 'businessAgreement/getAgreementById', payload: {id: record.id}});
        if(this.editBusinessagreement)this.editBusinessagreement.handleOpen(type);
    };

    handleSearch = () => {
        let values = this.props.form.getFieldsValue();
        let newPage = this.props.businessAgreement.page;
        newPage.page = 1;
        this.props.dispatch({
            type: 'businessAgreement/getAgreementlList',
            payload: {
                page: newPage,
                cstid: this.props.cstid,
                status: values.status,
            }
        });
    };


    columns = [
        {title: '标题', dataIndex: 'title', key: 'title'},
        {title: '正文', dataIndex: 'content', key: 'content'},
        {title: '客户', dataIndex: 'cstidName', key: 'cstidName'},
        {title: '提交人', dataIndex: 'creatorName', key: 'creatorName'},
        {title: '提交时间', dataIndex: 'createtime', key: 'createtime'},
        {title: '操作', key: 'key_oprator',
            render: (text, record, index) => {
                return (
                    <span>
                        {this.props.disabled == true || record.status == 2 || record.status == 4 || record.status == 5 ?
                            <span>
                                 <a href="#" onClick={this.onEdit.bind(this, record, "view")}>查看</a>
                            </span>
                            :
                            <span>
                                {record.status == 1 || record.status == 3 ?
                                    <span>
                                       <RightBox resource={RIGHT.SUPPLIER.SUPPLIER_MANAGE_AGREE_EDIT}> <a href="#" disabled={this.props.disabled}
                                           onClick={this.onEdit.bind(this, record, "edit")}>编辑</a>
                                        <span className="ant-divider"/></RightBox>

                                        <RightBox resource={RIGHT.SUPPLIER.SUPPLIER_MANAGE_AGREE_DELETE}>
                                             <Popconfirm placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除？'}
                                                         onConfirm={() => {
                                                             this.props.dispatch({
                                                                 type: 'businessAgreement/deleteAgreementById',
                                                                 payload: {
                                                                     id: record.id,
                                                                     cstid: record.cstid,
                                                                     onSuccess:()=>{
                                                                         this.handleSearch();
                                                                     }
                                                                 }
                                                             })
                                                         }} okText="是" cancelText="否">
                                                  <a href="#" disabled={this.props.disabled} >删除</a>
                                            </Popconfirm>
                                        </RightBox>
                                    </span>
                                    : ""}

                            </span>
                        }
                    </span>
                );
            }
        }];

    render() {
        const { getFieldDecorator } = this.props.form;
        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;
        let formItemLayout = {
            labelCol: { xs: { span: 24 }, sm: { span: 6 } },
            wrapperCol: { xs: { span: 24 }, sm: { span: 14 } }
        };
        let values = this.props.form.getFieldsValue();
        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            pageSize: this.props.businessAgreement.page.size,
            total: this.props.businessAgreement.page.totalRow,
            showSizeChanger: true,
            current: this.props.businessAgreement.page.page,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.businessAgreement.page;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'businessAgreement/getAgreementlList',
                    payload: {
                        id: R.isNil(dataSource.id) ? "" : dataSource.id,
                        page: newPage,
                        cstid: this.props.cstid
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.businessAgreement.page;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'businessAgreement/getAgreementlList',
                    payload: {
                        id: R.isNil(dataSource.id) ? "" : dataSource.id,
                        page: newPage,
                        cstid: this.props.cstid
                    }
                });
            }
        }
        return (
            <div>
                <Form>
                    <Row>
                        <Col span={7}>
                            <FormItem {...formItemLayout} label={'状态'}>
                                {getFieldDecorator('status', {
                                    initialValue: "1"
                                })(
                                    <RadioGroup style={{width: 800}}>
                                        <Radio value="1" >待申请</Radio>
                                        <Radio value="2" >待审核</Radio>
                                        <Radio value="4" >审核通过</Radio>
                                        <Radio value="5" >审核不通过</Radio>
                                    </RadioGroup>
                                )}
                            </FormItem>
                        </Col>

                    </Row>
                    <Row>
                        <Col span={3} style={{float: 'left'}}>
                            <Button type="primary" icon="search" style={{marginLeft: 10, marginBottom:20}} onClick={this.handleSearch.bind(this)}>查询</Button>
                        </Col>
                        <Col span={3} >
                            {(
                                this.props.dataSource.status == 4 ||this.props.type1 == 2||this.props.type1 == 4||this.props.type1 == 5 //客户审核过了  客户待审核  客户审核过 客户审核不通过
                            )?
                                "" :
                                <RightBox resource={RIGHT.SUPPLIER.SUPPLIER_MANAGE_AGREE_ADD}><Button type="primary"
                                                                                                  icon="plus"
                                                                                                  style={{marginLeft: 10}}
                                                                                                  onClick={this.handleOpenAdd.bind(this)}>新增采购合同</Button></RightBox>
                           }

                        </Col>

                    </Row>
                    <Row>
                        <Col span={3} >
                            <p  style={{color:'red',fontSize: 15}}> 注：业务合同暂时只允许新增一份，如需更改请联系业务总监。客户在审核中及审核通过后不允许新增合同。</p>
                        </Col>
                    </Row>

                </Form>
                <Table dataSource={this.props.businessAgreement.list} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} scroll={{x:true}}/>

                <EditBusinessagreement wrappedComponentRef={(inst) => this.editBusinessagreement = inst}
                                       dispatch={this.props.dispatch}
                                       disabled={this.props.disabled}
                                       cstid={this.props.cstid}
                                       handleSearch = {this.handleSearch}
                                       fkManager={this.props.fkManager}
                                       {...this.props.businessAgreement}
                />

            </div>
        )
    }
})

